package com.wang.myapplication.base;

/**
 * Created by xhwang on 2018/3/14.
 */

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class BaseFragment extends Fragment implements View.OnClickListener {
    private boolean isDebug=true;
    private String APP_NAME="Wangxq";
    protected final String TAG = this.getClass().getSimpleName();
    public View mContextView = null;
    public Activity mActivity = null;
    public Context mContext=null;
    private Unbinder mUnbinder;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        $Log(TAG + "-->onCreate()");
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        $Log(TAG + "-->onCreate()");
    }


    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        $Log(TAG + "-->onCreateView()");
        mContextView = inflater.inflate(bindLayout(), container, false);
        mUnbinder = ButterKnife.bind(this, mContextView);
        mActivity = getActivity();
        mContext = getContext();
        initView(mContextView);
        doBusiness(getActivity());
        return mContextView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        $Log(TAG + "-->onDestroyView()");
        mUnbinder.unbind();
    }

    @Override
    public void onResume() {
        super.onResume();
        $Log(TAG + "-->onResume()");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        $Log(TAG + "-->onDestroy()");
    }

    /**
     * [绑定布局]
     *
     * @return
     */
    public abstract int bindLayout();

    /**
     * [初始化控件]
     *
     * @param view
     */
    public abstract void initView(final View view);

    /**
     * [业务操作]
     *
     * @param mContext
     */
    public abstract void doBusiness(Context mContext);

    /**
     * View点击
     **/
    public abstract void widgetClick(View v);


    @Override
    public void onClick(View v) {
        if (fastClick())
            widgetClick(v);
    }

    @SuppressWarnings("unchecked")
    public <T extends View> T $(View view, int resId) {
        return (T) view.findViewById(resId);
    }

    /**
     * [日志输出]
     *
     * @param msg
     */
    protected void $Log(String msg) {
        if (isDebug) {
            Log.d(APP_NAME, msg);
        }
    }

    /**
     * [防止快速点击]
     *
     * @return
     */
    long lastClick = 0;

    private boolean fastClick() {

        if (System.currentTimeMillis() - lastClick <= 1000) {
            return false;
        }
        lastClick = System.currentTimeMillis();
        return true;
    }
}